package com.dnfm.game.equip.common;

import com.dnfm.game.equip.mapper.EquipAttributeMapper;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import javax.sql.DataSource;
import java.sql.*;

public class MapperService {

    public final static EquipAttributeMapper equipAttributeMapper;
    static DataSource dataSource;

    static {
        HikariConfig config = new HikariConfig();
//        config.setJdbcUrl("jdbc:sqlite:/Users/eisen/Desktop/Projects/code-src/game-dnfm/src/main/resources/dnfm.equip.db");
        config.setJdbcUrl("jdbc:sqlite:/Users/xieyuxin/Desktop/Projects/code-src/game-dnfm/src/main/resources/dnfm.equip.db");
        config.setDriverClassName("org.sqlite.JDBC");
        config.setUsername("");
        config.setPassword("");
        config.setMaximumPoolSize(10);
        dataSource = new HikariDataSource(config);

        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("dev", transactionFactory, dataSource);
        Configuration configuration = new Configuration(environment);
        configuration.addMapper(EquipAttributeMapper.class);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
        equipAttributeMapper = sqlSessionFactory.openSession().getMapper(EquipAttributeMapper.class);
    }

    public static void main(String[] args) throws SQLException {
        Connection connection = dataSource.getConnection();
        Statement statement =  connection.createStatement();
        ResultSet set = statement.executeQuery("select * from equip_attribute where scheme_id = 1");

        while (set.next()) {
            System.out.println(set.getString(3));

        }

    }


}
